<template>
  <div class="myDialog">
    <!-- 弹窗组件 -->
    <el-dialog :visible.sync="dialogFormVisible" :title="title" :before-close="closeDialog" :close-on-click-modal="false" width="450px">
      <el-form
        ref="ruleForm"
        :model="form"
        :rules="rules"
        :disabled="isDisabled"
        label-width="120px"

      >
        <el-form-item :label="$t('customer')" prop="custName">
          <el-input v-model.trim="form.custName" clearable style="width: 100%"/>
        </el-form-item>
        <el-form-item :label="$t('packageType')" prop="packageType">
          <el-input v-model.trim="form.packageType" clearable style="width: 100%"/>
        </el-form-item>
        <el-form-item :label="$t('Inspectionlevel')" prop="inspectionLevel">
          <el-select v-model.trim="form.inspectionLevel" clearable style="width: 100%">
            <el-option
              v-for="item in levelList"
              :key="item.value"
              :label="item.label"
              :value="item.value"/>
          </el-select>
        </el-form-item>
        <el-form-item :label="$t('inspectionFormName')" prop="formName">
          <el-input v-model.trim="form.formName" clearable style="width: 100%"/>
        </el-form-item>
      </el-form>
      <div v-if="!isDisabled" slot="footer" class="dialog-footer">
        <el-button icon="el-icon-circle-close" @click="closeDialog">{{
          $t("cancel")
        }}</el-button>
        <el-button v-btnThrottle="doSave" type="primary" icon="el-icon-receiving">{{ $t('save') }}</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import crudJob from '@/api/production/inspectionLevel'
import mySetting from '@/mixins/mySetting'
export default {
  name: 'InspectionLevelForm',
  mixins: [mySetting],
  data() {
    return {
      dialogFormVisible: false,
      title: '',
      form: {
        packageType: 'ALL'
      },
      isDisabled: false,
      rules: {
        custName: [
          { required: true, message: `${this.$t('customer')}${this.$t('isRequired')}`, trigger: 'blur' }
        ],
        packageType: [
          { required: true, message: `${this.$t('packageType')}${this.$t('isRequired')}`, trigger: 'blur' }
        ],
        inspectionLevel: [
          { required: true, message: `${this.$t('inspectionLevel')}${this.$t('isRequired')}`, trigger: 'change' }
        ]
      }, // 表单校验规则
      levelList: Object.freeze([{
        value: 'I',
        label: 'I'
      }, {
        value: 'II',
        label: 'II'
      }, {
        value: 'III',
        label: 'III'
      }, {
        value: 'S-1',
        label: 'S-1'
      }, {
        value: 'S-2',
        label: 'S-2'
      }, {
        value: 'S-3',
        label: 'S-3'
      }, {
        value: 'S-4',
        label: 'S-4'
      }])
    }
  },
  methods: {
    // 新增数据
    doSave() {
      this.$refs['ruleForm'].validate((valid) => {
        if (!valid) {
          return false
        }
        if (this.form.id) {
          // 存在id则是更新
          crudJob.edit(this.form).then(res => {
            this.$betterTips(res, this.closeDialog, this.handleUpdate)
          })
        } else {
          // 不存在id则是新增
          crudJob.add(this.form).then(res => {
            this.$betterTips(res, this.closeDialog, this.handleUpdate)
          })
        }
      })
    },
    // 打开弹窗
    openDialog(row) {
      if (row && row.id) {
        this.title = this.$t('detailinspectionLevel')
        this.isDisabled = false
        crudJob.get(row.id).then(res => {
          this.form = res.data
        })
      } else {
        this.title = this.$t('addinspectionLevel')
      }
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs.ruleForm.clearValidate()
      })
    },
    // 关闭弹窗
    closeDialog() {
      Object.assign(this.$data, this.$options.data.call(this))
    },
    handleUpdate() {
      this.$emit('initList')
    }
  }
}
</script>
